import datetime,cgi
import sys
import os
from google.appengine.ext.webapp import template
from google.appengine.ext import db
from google.appengine.ext import webapp
from google.appengine.api import users
from google.appengine.ext.webapp.util import run_wsgi_app
from google.appengine.dist import use_library
from os import environ
from models import *

class ProjectPool(webapp.RequestHandler):
	def verifyLogin(self):
		user = users.get_current_user()
		if user:
			usercheck=userDetails.all()
			usercheck.filter("email =",user.email())
			try:
				currentuser=usercheck.fetch(1)[0]
				userdet=db.GqlQuery("SELECT * FROM userDetails WHERE email='"+user.email()+"'")
				for i in userdet:
					if i.privilege=='admin':
						return
					elif i.approved==False:
						self.redirect("/usererror")
						return
				self.redirect("/user")
			except:
				path = os.path.join(os.path.dirname(__file__),'form.html')#use template
				self.response.out.write(template.render(path,{"email":user.email()}))#using template
		else:
			self.redirect(users.create_login_url(self.request.uri))

	def get(self):
		self.verifyLogin()
		projectpool=[]
		projectsinpool=db.GqlQuery("SELECT * FROM projectPool")
		for proj in projectsinpool:
			tmp=[]
			tmp.append(proj.poolprojectid)
			tmp.append(proj.projectname)
			projectpool.append(tmp)
		path = os.path.join(os.path.dirname(__file__),'projectpool.html')#use template
		self.response.out.write(template.render(path, {"projectpool":projectpool}))#using template

application = webapp.WSGIApplication([('/projectpool', ProjectPool)], debug=True)

def main():
	run_wsgi_app(application)

if __name__ == "__main__":
	main()
